package com.example.associationmanager.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.associationmanager.bean.Activity;
import com.example.associationmanager.bean.Funds;
import com.example.associationmanager.bean.Msg;
import com.example.associationmanager.bean.User;
import com.example.associationmanager.service.ActivityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Slf4j
@Controller
public class ActivityController {//社团活动接口
    @Autowired
    ActivityService activityService;
    @ResponseBody
    @GetMapping("/select/activity/all")//获取所有社团活动
    public Object getActivity(Integer page , Integer limit, HttpServletRequest request) {
        User user= (User) request.getSession().getAttribute("loginUser");//从session中获取用户属于的社团
        Integer communityNumber= user.getCommunityNumber();
        Page<Activity> associationPage=new Page<>(page, limit);
        Page<Activity> pageObj = activityService.page(associationPage,new QueryWrapper<Activity>().eq("activity_association_id",communityNumber));
        List<Activity> associationAll = pageObj.getRecords();//获取分页查询后的数据
        Msg msg = new Msg(0,"",activityService.count(), associationAll);
        return msg;

    }
    @ResponseBody
    @PostMapping("/activity/add")//添加单条社团活动
    public Object  ActivityAdd(String activityTitle, String activityPlace, String activityTime,String activityContent,Integer activityAssociationId) {
        String activityStatus="申请中";//默认新增社团活动状态为申请中
        Activity activity=new Activity(null,activityTitle, activityPlace,activityTime,activityContent,activityAssociationId,activityStatus);
        activityService.save(activity);//向数据库进行活动对象的插入
        log.info("日志打印："+"/activity/add社团活动添加接口"+"活动添加成功");
        Msg msg = new Msg(200,"success",null,null);

        return msg;

    }
    @ResponseBody
    @GetMapping ("/activity/delete")//删除单条社团活动
    public Object peopleDelete(@RequestParam("activityId") String activityId) {
        activityService.removeById(activityId);//删除活动
        log.info("日志打印："+"/activity/delete社团活动删除接口 "+"活动添删除成功");
        Msg msg = new Msg(200,"success",null,null);
        return msg;
    }
    @ResponseBody
    @GetMapping ("/activity/apply/pass")//活动项目审批通过
    public Object fundsApplyPass(@RequestParam("activityId") Integer activityId) {
        String activityStatus="已通过";
        Activity activity=new Activity(activityId,null,null,null,null,null,activityStatus);
        activityService.updateById(activity);
        Msg msg = new Msg(200,"success",null,null);
        return msg;
    }
    @ResponseBody
    @GetMapping ("/activity/apply/no")//活动项目审批不通过
    public Object fundsApplyNo(@RequestParam("activityId") Integer activityId) {
        String activityStatus="已拒绝";
        Activity activity =new Activity(activityId,null,null,null,null,null,activityStatus);
        activityService.updateById(activity);
        Msg msg = new Msg(200,"success",null,null);
        return msg;
    }

}
